package com.test;

import com.AutoVenderMain;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.config.WXMiniProgramConfig;
import com.dao.DeviceBoxMapper;
import com.dao.OrdersMapper;
import com.dao.SysUserMapper;
import com.dto.WXAccessTokenDTO;
import com.model.DeviceBox;
import com.model.Orders;
import com.model.SysUser;
import com.service.MiniProgramService;
import com.service.SettleService;
import com.service.impl.MiniProgramServiceImpl;
import com.service.impl.SettleServiceImpl;
import com.utils.DateUtil;
import com.utils.httpclient.HttpClientAssisstant;
import javafx.application.Application;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

@RunWith(SpringRunner.class)
@SpringBootTest(classes = AutoVenderMain.class)
public class test {

    @Resource
    DeviceBoxMapper deviceBoxMapper;


    @Autowired
    private OrdersMapper ordersMapper;

    @Autowired
    private MiniProgramServiceImpl miniProgramService;

    @Autowired
    private SettleServiceImpl settleService;

    @Autowired
    private SysUserMapper  sysUserMapper;

    @Test
    public void sem(){
        System.out.println("123");
        DefaultProfile profile = DefaultProfile
                .getProfile(
                        "cn-hangzhou",
                        "LTAIKb1PhNoEKZBJ",
                        "KvIOY2GZjiJyz3CFgxFZ6EihO8q3Mz"
                );
        IAcsClient client = new DefaultAcsClient(profile);

        CommonRequest request = new CommonRequest();
        //request.setProtocol(ProtocolType.HTTPS);
        request.setMethod(MethodType.POST);
        request.setDomain("dysmsapi.aliyuncs.com");
        request.setVersion("2017-05-25");
        request.setAction("SendSms");
        request.putQueryParameter("SignName","autohospital");
        request.putQueryParameter("RegionId", "cn-hangzhou");
        request.putQueryParameter("PhoneNumbers", "18811464173");
        request.putQueryParameter("TemplateCode","SMS_167396788");

        JSONObject param = new JSONObject();
        param.put("code","8888");
        request.putQueryParameter("TemplateParam",param.toJSONString());
        try {
            CommonResponse response = client.getCommonResponse(request);
            System.out.println(response.getData());
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
    }


    /**
     * sessIon_key解析
     */

    @Test
    public void  getWXMiniProgramAccessToken() throws  Exception{
        Map<String,String> getAccessTokenParam=new HashMap<String,String>();
        getAccessTokenParam.put("grant_type",WXMiniProgramConfig.ACCESS_TOKEN_GRANT_TYPE);
        getAccessTokenParam.put("appid", WXMiniProgramConfig.AppId);
        getAccessTokenParam.put("secret",WXMiniProgramConfig.AppSecret);
        String wxResponse = HttpClientAssisstant.doGet(WXMiniProgramConfig.WX_URL_GET_ACCESS_TOKEN, getAccessTokenParam, "utf-8");
        WXAccessTokenDTO wxAccessTokenDTO = JSONObject.parseObject(wxResponse, WXAccessTokenDTO.class);
        System.out.println("微信的返回结果是: "+wxResponse);
       // return  wxAccessTokenDTO.getAccess_token();
    }

    @Test
    public void selectBoxByDeviceIdAndProductIdNoStock(){
        DeviceBox productBox = deviceBoxMapper.selectBoxByDeviceIdAndProductIdNoStock((long)15, (long)4);
        System.out.println("微信的返回结果是: "+JSON.toJSONString(productBox));
    }

    @Test
    public void minTest() throws Exception{
     /*   String lastMonth = DateUtil.getLastMonth();
        SysUser  condition=new SysUser();
        condition.setUserType(2);
        List<SysUser> sysUsers = sysUserMapper.selectByCondition(condition);
        Optional.ofNullable(sysUsers).ifPresent(e->{
            List<Long> collect = sysUsers.stream().map(SysUser::getId).collect(Collectors.toList());
            for (Long userId : collect) {
                try {
                    settleService.generateSettlementOrderForAgent(userId, lastMonth);
                }catch (Exception ex){

                }

            }
        });
*/
    }

}
